<?php
class Helper_Advert
{
    /**
     * Get path to archive for section
     *
     * @static
     * @param Zend_Db_Table_Row|int $section
     * @return string
     */
    static function getArchivePath($resourceId, $year = null, $month = null)
    {
        if (null === $year) {
            $year = date('Y');
        }
        if (null === $month) {
            $month = date('m');
        }
        return BASE_PATH . '/archive/' . "$year-$month" . '/' . $resourceId . '/';
    }

    /**
     * Get hash name format
     *
     * @static
     * @return string
     */
    static function getHashNameFormat($day = null)
    {
        if (null === $day) {
            $day = date('d');
        }
        return $day . '-%s';
    }

    /**
     * Format phone
     *
     * @static
     * @param int|string $phone
     * @return string
     */
    static public function formatPhone($phone)
    {
        if ($phone) {
            preg_match('/([0-9]{2})?([0-9]{2,3})([0-9]{2})([0-9]{1}[0-9])(.*)/', $phone, $matches);
            unset($matches[0]);
            if ($matches) {
                if ($matches[1]) {
                    $matches[1] = '0' . $matches[1];
                } else {
                    unset($matches[1]);
                }
                $last = array_pop($matches);
                $phone = implode('-', $matches) . $last;
            }
        }
        return $phone;
    }

    /**
     * Format phone for query in database
     *
     * @static
     * @param int|string $phone
     * @return string
     */
    static public function dbFormatPhone($phone)
    {
        if ($phone) {
            preg_match_all('/([0-9]*)/', $phone, $matches);
            if (!empty($matches[0])) {
                $phone = implode('', $matches[0]);
            }
        }
        return $phone;
    }
}

